
package jrain.fw.http.common.rest.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jrain.fw.core.business.BusinessConst;
import jrain.fw.core.business.BusinessRequest;
import jrain.fw.core.rest.RestMethod;
import jrain.fw.core.rest.RestService;
import jrain.utils.collection.CollectionUtils;
import jrain.utils.lang.LangUtils;

/**
 * <pre>
 * 作者：3244924214@qq.com 
 * 描述：RestAciton
 * </pre>
 */
@RestService()
public class RestAction extends BaseAction {

	//private static final Logger log = LoggerFactory.getLogger(RestAction.class);
	
	public RestAction() {

	}

	@RestMethod(name = "/rest/*")
	public void rest(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
		String url = httpRequest.getRequestURI();	
		int clength = httpRequest.getContextPath().length();
		url = url.substring(clength);
		
		List<String> list = CollectionUtils.valueOfList(url, "/");
		if (list.size() == 3 || list.size() == 4) {
			String service = list.get(1);
			String method = list.get(2);
			String version = list.size() == 3 ? "v1" : list.get(3);
			BusinessRequest request = new BusinessRequest(service, method, version);
			request.putData(BusinessConst.EXT_TRACE_ID, LangUtils.randomUUID());
			doAction(request, httpRequest, httpResponse);
		} else {
			write404(httpRequest, httpResponse);
		}
	}

}
